﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Omes.Admin
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var resultstr = string.Empty;
            string fileflag = HttpContext.Current.Request.QueryString["fileflag"];
            switch (fileflag)
            {
                case "sigle":
                    {
                        resultstr = this.UploadFile("UploadFile/SiglePhotoText");
                    } break;
                case "imagelist":
                    {
                        resultstr = this.UploadFile("UploadFile/ImageList");
                    } break;
                default:
                    {
                        resultstr = this.UploadFile("UploadFile/SiglePhotoText");
                    }break;
            }
            Response.Write(resultstr);
            Response.End();
        }

        private string UploadFile(string filePath)
        {
            string resultstr = "{{\"err\":\"{0}\",\"msg\":\"{1}\"}}";
            if (HttpContext.Current.Request.ContentLength > 512000)
            {
                resultstr = string.Format(resultstr, "图片不能大于500k", "");
            }
            else
            {
                byte[] filebytes= null;
                var fileName = string.Empty;
                var filedate = HttpContext.Current.Request.Params["HTTP_CONTENT_DISPOSITION"];
                if (filedate == null)
                {
                    filedate = HttpContext.Current.Request.Files["filedata"].FileName;
                    filebytes = new byte[Request.Files["filedata"].InputStream.Length];
                    HttpContext.Current.Request.Files["filedata"].InputStream.Read(filebytes,0,(int)HttpContext.Current.Request.Files["filedata"].InputStream.Length);
                    fileName = filedate.Split('\\').LastOrDefault();
                }
                if (fileName == string.Empty)
                {
                    filedate = HttpUtility.UrlDecode(filedate, HttpContext.Current.Request.ContentEncoding);
                    var reg = new System.Text.RegularExpressions.Regex("filename=\"(.+\\..+?)\"");
                    var match = reg.Match(filedate);

                    if (match.Success)
                    {
                        fileName = match.Groups[1].Value;
                        filebytes = HttpContext.Current.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
                    }
                    else
                    {
                        resultstr = string.Format(resultstr, "文件名不合法", "");
                    }

                }
                var serverpath = Server.MapPath("/" + filePath + "/");
                if (fileName.Length > 10)
                {
                    resultstr = string.Format(resultstr, "文件名过长", "");
                }
                else if (System.IO.File.Exists(serverpath + fileName))
                {
                    resultstr = string.Format(resultstr, "文件名以存在，请重新检查", "");
                }
                else
                {

                    try
                    {
                        System.IO.File.WriteAllBytes(serverpath + fileName, filebytes);
                        resultstr = string.Format(resultstr, "", "/" + filePath + "/" + fileName);
                    }
                    catch
                    {
                        resultstr = string.Format(resultstr, "文件保存失败，请联系管理员", "");
                    }
                }
            }
            return resultstr;
        }
    }
}